#include <iostream>
#include <string>
#include <queue>
using namespace std;

struct Node {
	string val;
	Node* left = nullptr;
	Node* right = nullptr;
};
string sp;
Node* H = nullptr;
//void getTree(Node*& h)
//{
//	string cur;
//	cin >> cur;
//	if (cur == sp) return;
//	h = new Node;
//	h->val = cur;
//	getTree(h->left);
//	getTree(h->right);
//}
void getTree(Node*& h)
{
	h = new Node;
	queue<Node*> q;
	cin >> h->val;
	q.push(h);
	while (q.size()) {
		int k = q.size();
		while (k--) {
			Node* cur = q.front(); q.pop();
			string l, r; 
			if (!(cin >> l))break;
			if (l != sp) {
				cur->left = new Node;
				cur->left->val = l;
				q.push(cur->left);
			}
			if (!(cin >> r))break;
			if (r != sp) {
				cur->right = new Node;
				cur->right->val = r;
				q.push(cur->right);
			}
		}
	}
}
void prev(Node* h, Node* gp)
{
	if (!h) return;
	if (h != gp) cout << ',';
	cout << h->val;
	prev(h->left, gp);
	prev(h->right, gp);
}
bool j = false;
void in(Node* h)
{
	if (!h) return;
	in(h->left);
	if (j) cout << ',';
	cout << h->val; j = true;
	in(h->right);
}
void post(Node* h, Node* gp)
{
	if (!h) return;
	post(h->left, gp);
	post(h->right, gp);
	cout << h->val;
	if (h != gp) cout << ',';
}
int main()
{
	cin >> sp;
	getTree(H);
	prev(H, H); cout << endl;
	in(H); cout << endl;
	post(H, H);
	return 0;
}
//+ 
//- 62 
//* / # # 
//+ 3 400 + 
//12 14 # # # # 30 * 
//# # # #     # # 10 5 